/*
 *  Copyright 2009 David Kuhn <david.s.kuhn at gmail.com>.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package com.google.code.teamtools.domain.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.code.teamtools.domain.IPerson;
import com.google.code.teamtools.domain.IResourceOwner;
import com.google.code.teamtools.domain.IRole;
import com.google.code.teamtools.domain.ISkill;

/**
 * Person implementation.
 * 
 * @author David Kuhn
 */
public abstract class Person extends AbstractResource implements IPerson
{
    private static final Logger LOG = LoggerFactory.getLogger(Person.class);
    
    private IRole primaryRole;
    private ISkill primarySkill;
    private List<IRole> roles = new ArrayList<IRole>();
    private List<ISkill> skills = new ArrayList<ISkill>();

    protected Person()
    {
        super();
    }
    
    protected Person(Long id)
    {
        super(id);
    }
    
    protected Person(String name)
    {
        super(name);
    }
    
    protected Person(String name, IResourceOwner owner)
    {
        super(name, owner);
    }

    public void addRole(IRole role)
    {
        // Check that the person hasn't already got the role.
        if (!roles.contains(role)) {
            roles.add(role);
            // If a primary role hasn't been set then set one.
            if (primaryRole == null)
                setPrimaryRole(role);
        }
    }

    public void addRoleAsPrimary(IRole role)
    {
        addRole(role);
        setPrimaryRole(role);
    }

    public void addSkill(ISkill skill)
    {
        // Check that the person hasn't already got the skill.
        if (!skills.contains(skill)) {
            skills.add(skill);
            // If a primary skill hasn't been set then set one.
            if (primarySkill == null)
                setPrimarySkill(skill);
        }
    }

    public void addSkillAsPrimary(ISkill skill)
    {
        addSkill(skill);
        setPrimarySkill(skill);
    }

    public IRole getPrimaryRole()
    {
        return primaryRole;
    }

    public ISkill getPrimarySkill()
    {
        return primarySkill;
    }

    public List<IRole> getRoles()
    {
        return roles;
    }

    public List<ISkill> getSkills()
    {
        return skills;
    }

    public void setPrimaryRole(IRole role)
    {
        if (roles.contains(role))
            primaryRole = role;
        else
            throw new IllegalArgumentException("Role [" + role.toString()
                    + "] not found for person [" + this.toString()
                    + "]. Have you forgotten to add the role first?");
    }

    public void setPrimarySkill(ISkill skill)
    {
        if (skills.contains(skill))
            primarySkill = skill;
        else
            throw new IllegalArgumentException("Skill [" + skill.toString()
                    + "] not found for person [" + this.toString()
                    + "]. Have you forgotten to add the skill first?");
    }
    
    public String toString()
    {
        StringBuilder sb = new StringBuilder();        
        sb.append("Person[");
        sb.append("id:");
        sb.append(this.getId());
        sb.append("|name:");
        sb.append(this.getName());
        sb.append("|owner:");
        sb.append(String.valueOf(this.getOwner()));
        sb.append("|primaryRole:");
        sb.append(this.getPrimaryRole());
        sb.append("|roles:[");
        sb.append(this.getRoles().toString());
        sb.append("]|primarySkill:");
        sb.append(this.getPrimarySkill());
        sb.append("|skills:[");
        sb.append(this.getSkills().toString());
        sb.append("]]");
        
        return sb.toString();
    }
}
